﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using University.Workshop.Presenters;

namespace University.Workshop.Presentation
{
    /// <summary>
    /// Interaction logic for Register.xaml
    /// </summary>
    public partial class Register : Page, IRegisterExpenseView
    {
        MainWindow mw;
        PresenterRegisterExpense pRegisterExpense;
        Model.User u;

        public Register(Model.User user, MainWindow mW)
        {
            mw = mW;
            u = user;

            InitializeComponent();
            pRegisterExpense = new PresenterRegisterExpense(this, user);
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            App.Current.MainWindow.Title = "Registrar Egreso";
        }

        public void SystemMessage(string MessageValue)
        {
            MessageBox.Show(App.Current.MainWindow, MessageValue, "Atencion!!!", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK, MessageBoxOptions.RightAlign);
        }

        public void ErrorMessage(string MessageValue)
        {
            MessageBox.Show(App.Current.MainWindow, MessageValue, "Error!!!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.RightAlign);
        }

        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.Navigate(new Expenses.SearchExpense());
        }

        private void RegisterBtn_Click(object sender, RoutedEventArgs e)
        {
            bool seModifico;
            seModifico = pRegisterExpense.RegisterExpense();

            if (seModifico)
            {
                MessageBox.Show("El egreso fue registrado exitosamente.");
                this.NavigationService.Navigate(new Register(u, mw));
            }

        }

        private void NumsEspacioTxt_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
        }

        private void SoloNums_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            foreach (char ch in e.Text)
            {
                e.Handled = !(char.IsDigit(ch));
            }
        }

        /*private void Factura_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            foreach (char ch in e.Text)
            {
                e.Handled = !(char.IsDigit(ch)) && !(ch.Equals('-'));
            }
        }*/

        private void CantCupyPasteOrCut(object sender, CanExecuteRoutedEventArgs e)
        {
            if (e.Command == ApplicationCommands.Copy)
                e.Handled = true;
            if (e.Command == ApplicationCommands.Cut)
                e.Handled = true;
            if (e.Command == ApplicationCommands.Paste)
                e.Handled = true;
        }

        #region IModifyUserPasswordView Members

        public DateTime SpendDate
        {
            get 
            {
                return DateTime.Parse(SpenDateDpk.Text);
            }
        }

        public decimal Price
        {
            get
            {
                if (PrinceTxt.Text != "")
                    return decimal.Parse(PrinceTxt.Text);
                else
                    return 0;
            }
        }

        public short Item
        {
            get
            {
                return (short)ItemCbx.SelectedIndex;
            }
        }

        public int Amount
        {
            get
            {
                if (AmountTxt.Text != "")
                    return int.Parse(AmountTxt.Text);
                else
                    return 0;
            }
        }

        public string Description
        {
            get
            {
                return DetailsTxt.Text;
            }
        }

        public string Receipt
        {
            get
            {
                return ReceiptTxt.Text;
            }
        }

        #endregion

        private void SpenDateDpk_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled = true;
        }
    }
}
